package com.example.jn_dxs.Ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

import com.example.jn_dxs.Base.BaseActivity;
import com.example.jn_dxs.Base.BaseViewModel;
import com.example.jn_dxs.R;
import com.example.jn_dxs.Ui.adapter.HomeFragmentPagerAdapter;
import com.example.jn_dxs.Ui.fragment.DemandFragment;
import com.example.jn_dxs.Ui.fragment.MapFragment;
import com.example.jn_dxs.Ui.fragment.MineFragment;
import com.example.jn_dxs.Ui.fragment.StatisticsFragment;
import com.example.jn_dxs.databinding.ActivityMainBinding;

public class MainActivity extends BaseActivity<BaseViewModel, ActivityMainBinding> {

    private Long firstExitClickTime = Long.valueOf(0);

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initEventAndView() {
        binding.BottomNavigationView.setItemIconTintList(null);
        binding.HomeViewPager.setOffscreenPageLimit(3);
        binding.HomeViewPager.setScroll(false);
        binding.HomeViewPager.setAdapter(new HomeFragmentPagerAdapter(getSupportFragmentManager(), new MapFragment(), new DemandFragment(), new StatisticsFragment(), new MineFragment()));
    }

    @Override
    public void initClick() {
        binding.BottomNavigationView.setOnNavigationItemSelectedListener(menuItem -> {
            switch (menuItem.getItemId()) {
                case R.id.Map:
                    binding.HomeViewPager.setCurrentItem(0);
                    break;
                case R.id.Demand:
                    binding.HomeViewPager.setCurrentItem(1);
                    break;
                case R.id.Statistics:
                    binding.HomeViewPager.setCurrentItem(2);
                    break;
                case R.id.mine:
                    binding.HomeViewPager.setCurrentItem(3);
                    break;
            }
            return true;
        });

        binding.HomeViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        binding.BottomNavigationView.setSelectedItemId(R.id.Map);
                        break;
                    case 1:
                        binding.BottomNavigationView.setSelectedItemId(R.id.Demand);
                        break;
                    case 2:
                        binding.BottomNavigationView.setSelectedItemId(R.id.Statistics);
                        break;
                    case 3:
                        binding.BottomNavigationView.setSelectedItemId(R.id.mine);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }


    /**
     * 双击退出
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Long secondTime = System.currentTimeMillis();
            if (secondTime - firstExitClickTime > 2000) {
                Toast.makeText(this, "再点一次退出APP", Toast.LENGTH_SHORT).show();
                firstExitClickTime = secondTime;
                return true;
            }
            System.exit(0);
        }
        return false;
    }
}
